package com.loong.order.handler;

import com.loong.common.enumerate.OrderStatus;
import com.loong.common.enumerate.PaymentType;
import com.loong.common.utils.TransformUtils;
import com.loong.order.business.OrderBaseBusiness;
import com.loong.order.common.forehead.param.OrderBaseForeheadParam;
import com.loong.order.common.forehead.result.OrderBaseResult;
import com.loong.order.model.OrderBase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 订单基础服务
 * @Author: hzp
 * @Date: 2020-12-31 9:23
 */
@Slf4j
@Component
public class OrderBaseHandler {
    @Resource
    private OrderBaseBusiness orderBaseBusiness;

    /**
     * 保存订单基础信息
     * @param param 订单参数
     */
    public OrderBase save(OrderBaseForeheadParam param){
        OrderBase orderBase = TransformUtils.transform(param, OrderBase.class);
        //设置订单状态
        orderBase.setStatus(OrderStatus.WAITING_PAYMENT.getCode());
        //设置下单时间
        orderBase.setCreateOrderTime(new Date());
        orderBaseBusiness.save(orderBase);
//        param.setId(orderBase.getId());
        return orderBase;

    }

    public OrderBaseResult findById(Long id){
       return TransformUtils.transform(orderBaseBusiness.findById(id),OrderBaseResult.class);
    }

    /**
     * 更新订单信息
     * @param param 订单信息
     */
    public void update(OrderBaseForeheadParam param){
        OrderBase orderBase = TransformUtils.transform(param, OrderBase.class);
        //设置更新时间
        orderBase.setLastUpdateTime(new Date());
        orderBaseBusiness.updateByPrimaryKeySelective(orderBase);
    }

    /**
     * 更新订单信息
     * @param orderBase 订单信息
     */
    public void update(OrderBase orderBase){
        //设置更新时间
        orderBase.setLastUpdateTime(new Date());
        orderBaseBusiness.updateByPrimaryKeySelective(orderBase);
    }


    /**
     * 根据ids 删除
     * @param ids （list->（1,2,3）
     */
    public void deleteIds(List<Long> ids) {
        orderBaseBusiness.deleteByIds(StringUtils.join(ids,","));
    }

    /**
     * 根据id查询 OrderBase
     * @param id 订单id
     * @return OrderBase
     */
    public OrderBase findOrderBaseById(Long id) {
        return orderBaseBusiness.findById(id);
    }

    /**
     *  根据用户id、支付方式、订单状态 查询用户 货到付款 未支付配送中的订单数量
     * @param userId 用户id
     * @param cashOnDelivery 支付方式 货到付款
     * @param waitingPayment 订单状态 待支付 配送中
     * @return count
     */
    public Long countByCondition(Long userId, PaymentType cashOnDelivery, OrderStatus waitingPayment) {
        Condition condition = new Condition(OrderBase.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、支付方式、订单状态 查询用户 货到付款 未支付的订单数量
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("paymentType", cashOnDelivery.getCode());
        criteria.andEqualTo("status", waitingPayment.getCode());
        return orderBaseBusiness.count(()->orderBaseBusiness.findByCondition(condition));
    }
    /**
     *  根据用户id、支付方式、订单状态 查询用户 货到付款 未支付配送中的订单集合
     * @param userId 用户id
     * @param cashOnDelivery 支付方式 货到付款
     * @param waitingPayment 待支付 配送中
     * @return List<OrderBase>
     */
    public List<OrderBase> findByCondition(Long userId, PaymentType cashOnDelivery, OrderStatus waitingPayment){
        Condition condition = new Condition(OrderBase.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、支付方式、订单状态 查询用户 货到付款 未支付的订单数量
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("paymentType", cashOnDelivery.getCode());
        criteria.andEqualTo("status", waitingPayment.getCode());
        return orderBaseBusiness.findByCondition(condition);
    }
}
